package org.example;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * PDF导出
 * @author message丶小和尚
 * @create 2020/01/10
 */
@Controller
public class PdfController {

    @RequestMapping("exportPdfDemo")
    @ResponseBody
    public void exportPdfDemo(HttpServletResponse response, @RequestParam("param")String param) throws Exception{
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setImage(PdfUtil.PDF_DEMO_IMAGE);//设置图片,没有图片设置空即可
        resultDTO.setData(param);//参数
        resultDTO.setDate(DateFormatUtils.format(new Date(),"yyyy年MM月dd日"));//时间
        PdfUtil.createPDF(PdfUtil.PDF_DEMO_TEMPLATE,resultDTO,response.getOutputStream(),resultDTO.getImage());
    }

    @RequestMapping("exportPdfList")
    @ResponseBody
    public void exportPdfList(HttpServletResponse response, @RequestParam("param")String param) throws Exception{
        ResultDTO resultDTO = new ResultDTO();
        List<ResultVO> resultVOList = new ArrayList<>();
        //设置list值
        ResultVO resultVO = new ResultVO();
        resultVO.setDemo("JAVA");
        resultVOList.add(resultVO);
        ResultVO resultVO1 = new ResultVO();
        resultVO1.setDemo("PDF");
        resultVOList.add(resultVO1);
        ResultVO resultVO2 = new ResultVO();
        resultVO2.setDemo("我是中文");
        resultVOList.add(resultVO2);
        resultDTO.setQueryList(resultVOList);
        PdfUtil.createPDF(PdfUtil.PDF_DEMO_LIST_TEMPLATE,resultDTO,response.getOutputStream(),resultDTO.getImage());
    }
}
